package com.danceforyou.salary.controller;

import com.danceforyou.common.model.view.BizData4Page;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.util.StringUtil;
import com.danceforyou.salary.service.SalaryEntryService;
import com.danceforyou.salary.vo.SalaryEntryVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping(value = "/salary")
public class SalaryController {

    @Autowired
    private SalaryEntryService salaryEntryService;
    @Autowired(required = false)
    private HttpSession session;

    /**
     * 处理业绩查询条件
     * @param condition 查询条件
     */
    public void dealMediumCondition(Map<String, Object> condition) {
        for (Map.Entry<String, Object> entry : condition.entrySet()) {
            if (StringUtil.isBlank(entry.getValue().toString())) {
                condition.put(entry.getKey(), null);
            }
        }
        if (condition.get("belongUserId") != null && condition.get("belongUserId").toString().equals("ALL")) {
            condition.remove("belongUserId");
        }
    }

    /**
     * 分页查询我的业绩条目列表
     * @param bizData4Page BizData4Page
     * @return BizData4Page
     */
    @RequestMapping(value = "/querySalaryEntryPage", method = RequestMethod.GET)
    public BizData4Page<SalaryEntryVO> querySelfMediumPage(BizData4Page<SalaryEntryVO> bizData4Page) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        Map<String, Object> condition = bizData4Page.getCondition();
        dealMediumCondition(condition);
        if ((Constants.ROLE_MJB_CODE.equals(loginUser.getRole().getRoleType()) || Constants.ROLE_XSB_CODE.equals(loginUser.getRole().getRoleType())) && condition.get("belongUserId") != null) {
            condition.put("belongUserId", loginUser.getId());
        }
        salaryEntryService.queryPageByDataPerm(bizData4Page);
        return bizData4Page;
    }

}
